package com.mall.controller;

import com.mall.common.resp.ResponseResult;
import com.mall.entity.Carousel;
import com.mall.entity.Goods;
import com.mall.service.CarouseService;
import com.mall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-02-28 14:16
 */
@Api(tags = "轮播图")
@RestController
@RequestMapping("carouse")
public class CarouseController {

    @Autowired
    private CarouseService carouseService;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("添加轮播图")
    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated Carousel carousel){
        Goods goods = goodsService.getGoodsById(carousel.getGoodsId());
        if(ObjectUtils.isEmpty(goods)){
            return ResponseResult.error("goodsId对应商品不存在");
        }
        int count = carouseService.getShowCount();
        if(ObjectUtils.isNotEmpty(carousel.getDisplay()) && carousel.getDisplay() == 1 && count >= 6){
            return ResponseResult.error("轮播图展示不超过6个");
        }
        if (ObjectUtils.isNotEmpty(carouseService.getCarouseByGoodsId(carousel.getGoodsId()))) {
            return ResponseResult.error("此商品已经被关联");
        }
        return ResponseResult.toResponse(carouseService.add(carousel));
    }

    @ApiOperation("移除轮播图")
    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @PostMapping("remove")
    public ResponseResult remove(@RequestParam Integer carouselId){
        return ResponseResult.toResponse(carouseService.remove(carouselId));
    }

    @ApiOperation("修改轮播图")
    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @PostMapping("modify")
    public ResponseResult modify(@RequestBody Carousel carousel){
        if(ObjectUtils.isEmpty(carousel.getCarouselId())){
            return ResponseResult.error("id不能为null");
        }
        if(ObjectUtils.isNotEmpty(carousel.getGoodsId())){
            Goods goods = goodsService.getGoodsById(carousel.getGoodsId());
            if(ObjectUtils.isEmpty(goods)){
                return ResponseResult.error("goodsId对应商品不存在");
            }
            if (ObjectUtils.isNotEmpty(carouseService.getCarouseByGoodsId(carousel.getGoodsId()))) {
                return ResponseResult.error("此商品已经被关联");
            }
        }
        if(ObjectUtils.isNotEmpty(carousel.getDisplay()) && carousel.getDisplay() == 1){
            int count = carouseService.getShowCount();
            if(count >= 6){
                return ResponseResult.error("轮播图展示不超过6个");
            }
        }
        return ResponseResult.toResponse(carouseService.modify(carousel));
    }


    @ApiOperation("取出页面显示轮播")
    @GetMapping("list")
    public List<Carousel> list(){
        return carouseService.list();
    }


    @ApiOperation("取出所有")
    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @GetMapping("/all")
    public List<Carousel> all(){
        return carouseService.all();
    }
}